(defun primep(n)
  (cond 
    ((< n 0) (primep (- n)))
    ((<= n 3) t)
    ((zerop (mod n 2)) nil)
    (t (loop 
	  for i = 3 then (+ 2 i) 
	  with lim = (sqrt n) 
	  while (<= i lim) 
	  when (zerop (mod n i)) do (return nil) 
	  finally (return t)))))

(defun euler-func (a b x)
  (+ (expt x 2) (* a x) b))

(defun first-prime (a b)
  (loop for i = 1 then (1+ i)
       ;for j from 1 to 1000
       while (primep (euler-func a b i))
       finally (return i)))

(defun probl27()
  (loop for a from -999 to 999 
     with max = 0
     with maxa = 1
     with maxb = 1
     do (loop for b = -999 then (+ 2 b)
	   while (<= b 999)
	   when (and (primep b) (> (first-prime a b) max))
	   do (setq max (first-prime a b) maxa a maxb b))
     finally (return (list maxa maxb))))
